<?php

namespace app\beautiful\helper\payment;

use app\beautiful\logic\printer\PrinterLogic;
use app\common\helper\payment\PaymentBaseHelper;

use app\common\logic\SmsLogic;

use app\beautiful\model\order\GoodsOrderModel;

use app\beautiful\logic\LogsLogic;

use app\beautiful\model\member\MemberCouponModel;

use app\beautiful\logic\MemberWalletLogic;

use app\common\model\ShopGmvLogsModel;

use app\beautiful\logic\PartnerLogic;

use app\beautiful\model\setting\SettingSmsModel;



//每个子类只需要专注于自己的 respond 逻辑

class PaymentGoodsOrderHelper extends PaymentBaseHelper{

    protected  $moudel = 'beautiful';

    protected  $orderKey = 'mo'; //给一个KEY增大吞吐量

    protected  $respondAction = 'goodsMallRespond';

    

    public function respondCallback($orderId,$payInfo,$fail){

         $order = GoodsOrderModel::find($orderId);

        if(null === $order) return;      

        if($order->is_paid == 0){

            $order->is_paid  = 1;

            $order->pay_info = $payInfo;

            $order->pay_type = 'weixin';

            $order->status = $order->logistics_type == 1 ? 2 : 4; //订单的状态值

            if($order->logistics_type == 3) $order->status = 9; //订单的状态值

            $order->pay_time = time();

            $order->save();

            

            if(!empty($order->member_coupon_id)){ //优惠券的使用

                if(null !== $myCoupon = MemberCouponModel::find($order->member_coupon_id)){

                    if($myCoupon->status == 0){

                        $myCoupon->status = 1;

                        $myCoupon->save();

                        LogsLogic::coupon($order->shop_id, $order->member_id, -1, 12, $order->member_coupon_id, '', 0);

                    }

                }

            }

            $MemberWalletLogic = new MemberWalletLogic($order->shop_id, $order->member_id);

            if(!empty($order->use_integral)){ //使用积分为商城支付

               $MemberWalletLogic->useIntegral($order->use_integral, 9);

            }

            $needPay = round(($order->total_price * 100 + $order->freight * 100 - $order->coupon_money * 100 - $order->integral_balance * 100)/100,2);

            $MemberWalletLogic->addIntegralByPayment($needPay); //更新支付返的积分


            //分销记录

            $PartnerLogic = new PartnerLogic($order->shop_id);

            //$PartnerLogic->commission($order->member_id,$needPay);

            $PartnerLogic->commissionNew($order->member_id, $needPay, 'order', $orderId);


            //插入日志

            $shop_gmv_logs = new ShopGmvLogsModel();

            $shop_gmv_logs->shop_id=$order->shop_id;

            $shop_gmv_logs->balance=$needPay;

            $shop_gmv_logs->save();



            //发送短信给管理员

            $sms_setting = SettingSmsModel::where([['shop_id','=',$order->shop_id]])->find();

            if(null !== $sms_setting){

                if($sms_setting->order_notice_admin==1){

                    $sms_logic= new SmsLogic();

                    $params=[

                        'money'=>$needPay,

                    ];

                    $sms_logic->send($order->shop_id,$sms_setting->boss_mobiles,$params,'orderNoticeAdmin');

                }

            }

            //打印机
            if($order->logistics_type == 2){
                $type = 'self';
            }
            if($order->logistics_type == 3){
                $type = 'city';
            }
            if(in_array($order->logistics_type,[2,3])){

                $printer_logic = new PrinterLogic($order->shop_id);
                $printer_logic->print($type,$orderId);
            }

        }

    }

}